package com.example.Lezedata.ui.activity.BankCard;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.activity.common.TransitionPage;
import com.example.Lezedata.ui.activity.common.WebVc;
import com.example.Lezedata.util.GBUtil;

import org.w3c.dom.Text;

import java.util.ArrayList;

import bid.Bid;
import common.Common;


public class BankCardVc extends GBBaseVc {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bank_card_vc;
    }

    @Override
    public CharSequence title() {
        return "Bank Card";
    }

    ArrayList<Bid.BankCardInfo> models = new ArrayList<Bid.BankCardInfo>();
    private ListView tableView;
    BaseAdapter adapter;

    private View header;
    private TextView addButton;
    private String reference;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        tableView = findViewById(R.id.tableView);
        addButton = findViewByTag("add");

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCard();
            }
        });

        addDiverLine();
        addHeader();
        setAdapter();
        getCardList();
    }

    void addHeader(){
        header = LayoutInflater.from(mContext).inflate (R.layout.bank_card_no_data,null,false);
        tableView.addHeaderView(header);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    TextView addDiverLine(){
        TextView line = new TextView(mContext);
        line.setHeight(GBUtil.dpTo2px(mContext,10));
        line.setBackground(new ColorDrawable(getColor(R.color.app_color_inputBackground)));
        tableView.addHeaderView(line);
        return line;
    }

    void addCard(){
        ServerResponseObserver observer = new ServerResponseObserver(new ServerResponseObserver.requestListener<Bid.AddBankCardInfoResponse>() {
            @Override
            public void complete(Bid.AddBankCardInfoResponse value, GBError t) {
                if (t == null){
                    reference = value.getReference();
                    gotoBandCard(value.getUrl());
                }
            }
        });
        GBNetRequest.stubUser().addBankCardInfo(Common.EmptyRequest.newBuilder().build(),observer);
    }

    void gotoBandCard(String url){
        Intent intent = new Intent(mContext,WebVc.class);
        intent.putExtra("url",url);
        jumpActivityIntent(intent);
    }

    void setAdapter(){
        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                int count = models.size();
                if (count > 0){
                    tableView.removeHeaderView(header);
                    header = null;
                }else {
                    if (header == null){
                        addHeader();
                    }
                }
                return count;
            }

            @Override
            public Bid.BankCardInfo getItem(int position) {
                return models.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null){
                    convertView = BankCardVcCell.newInstance(mContext);
                }
                BankCardVcCell cell = (BankCardVcCell)convertView;
                cell.listener = new BankCardVcCell.ClassListener() {
                    @Override
                    public void delete(Bid.BankCardInfo cardInfo) {
                        deleteCard(cardInfo);
                    }

                    @Override
                    public void defaultSelect(Bid.BankCardInfo cardInfo) {
                        setDefaultCard(cardInfo);
                    }
                };
                cell.setCardInfo(getItem(position));
                return convertView;
            }
        };
        tableView.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (reference != null){
            checkBandResult();
        }
    }

    void checkBandResult(){
        ServerResponseObserver observer = new ServerResponseObserver(new ServerResponseObserver.requestListener<Common.CommonResponse>() {
            @Override
            public void complete(Common.CommonResponse value, GBError t) {
                if (t == null){
                    getCardList();
                }
            }
        });

//        GBNetRequest.stubUser().checkAddBankCardInfo(Bid.CheckAddBankCardInfoRequest.newBuilder().setReference(reference).build(),observer);
    }

    void getCardList(){
        ServerResponseObserver observer = new ServerResponseObserver(new ServerResponseObserver.requestListener<Bid.BankCardListResponse>() {
            @Override
            public void complete(Bid.BankCardListResponse value, GBError t) {
                if (t == null){
                    models.addAll(value.getListList());
                    adapter.notifyDataSetChanged();
                }
            }
        });
        GBNetRequest.stubUser().bankCardInfoList(Common.PageInfo.newBuilder().setPageSize(10).setPageStart(0).build(),observer);
    }

    void deleteCard(final Bid.BankCardInfo cardInfo){
        ServerResponseObserver observer = new ServerResponseObserver(new ServerResponseObserver.requestListener<Common.CommonResponse>() {
            @Override
            public void complete(Common.CommonResponse value, GBError t) {
                if (t == null){
                    models.remove(cardInfo);
                    adapter.notifyDataSetChanged();
                }
            }
        });
        GBNetRequest.stubUser().delBankCardInfo(Common.Id.newBuilder().setId(cardInfo.getId()).build(),observer);
    }

    void setDefaultCard(final Bid.BankCardInfo cardInfo){
        ServerResponseObserver observer = new ServerResponseObserver(new ServerResponseObserver.requestListener<Common.CommonResponse>() {
            @Override
            public void complete(Common.CommonResponse value, GBError t) {
                if (t == null){
//                    Bid.BankCardInfo info = cardInfo.toBuilder().setDefaultCard(Bid.SetDefault.SD_BY_DEFAULT).build();
//                    models.set(models.indexOf(cardInfo),info);
//                    adapter.notifyDataSetChanged();
                }
            }
        });
        GBNetRequest.stubUser().setBankCardDefault(Common.Id.newBuilder().setId(cardInfo.getId()).build(),observer);
    }
}